﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BaseSet.Data;
using BaseSet.Logic;
using BaseSet.Entity;
using BaseSet.Framework;
using BaseSet.Entity.Common;
using BaseSet.Entity.Common.Text;
using BaseSet;
using System.Data;

namespace WebSite.Company
{
    public partial class UserCompanyBlogList : PreBasePage
    {
        public string companyid = "";
        public string companyowneruserid = "";
        public string strseo = "";
        public string ownerlink = "";
        public string userid = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string id = Request.QueryString["id"] + "";
            userid = Request.QueryString["userid"] + "";
            if (id == "")
            {
                Response.Redirect("../Index.aspx");
                return;
            }
            else
            {
                companyid = id;
            }
            //userid = userid.Substring(0, userid.Length - 1);
            if (!Page.IsPostBack)
            {
                BindBlog();
                BindSeo();
                BindCareCompany();//组长关注的小组
                BindPVBlogOrder();//小组博客浏览榜
                BindCompanyUser();//加载小组用户

            }
        }

        #region 加载博客
        private void BindBlog()
        {
            //当前公司数据
            Companys cinfo = new Companys();
            CompanysHelper chelper = new CompanysHelper();
            cinfo = chelper.GetCompanys(companyid);
            litCompanyName.Text = cinfo.CompanyName;
            hidCompanyID.Value = companyid;
            hidObjectID.Value = companyid;
            litBrief.Text = cinfo.Brief;
            litOwnerNickName.Text = cinfo.OwnerNickName;
            hidOwnerUserID.Value = cinfo.OwnerUserID;
            companyowneruserid = cinfo.OwnerUserID;
            litUserNum.Text = cinfo.UserNum.ToString();
            litCareNum.Text = cinfo.CareNum.ToString();
            litBlogPVNum.Text = cinfo.BlogPVNum.ToString();

            ownerlink = "../u/" + cinfo.OwnerUserID + ".aspx";


            //用户的页面 加载该用户发表的博客
            //设置控件属性，从BasePage中得到
            PageConfig(BlogUPager);

            //List<Member> list = new List<Member();
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();

            //加载有效
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "CompanyID", companyid);
            if (!string.IsNullOrEmpty(userid))
            {
                c.Add(CriteriaType.Equals, "UsersID", userid);
            }

            BlogUPager.ItemCount = bhelper.QueryBlogCountBySearch(c);

            // 以下的值从BasePage中得到
            BlogUPager.UrlFormat = UrlFormat;
            //BlogUPager.PrefixText = GetPreFixText(BlogUPager);

            string[] fields = new string[] { "ID", "BlogBigType", "BlogTypeName", "BlogTypeID", "Title", "BlogContent", "UsersID", "UserRealmName", "NickName", "PVCount", "ReplyCount", "RecommandCount", "HasPic", "Flag", "CreateDate" };

            DataTable dt = new DataTable();
            dt = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, BlogUPager.Begin - 1, BlogUPager.Count, fields);

            dt.Columns.Add(new DataColumn("strCreateDate"));
            dt.Columns.Add(new DataColumn("strBlogContent"));
            dt.Columns.Add(new DataColumn("strHasPic"));
            dt.Columns.Add(new DataColumn("strVideoPic"));

            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    DateTime dtCreateDate = Convert.ToDateTime(dr["createdate"]);
                    dr["strCreateDate"] = dtCreateDate.ToString("yyyy-MM-dd HH:mm");

                    //截取内容
                    string blogcontent = Server.HtmlDecode(dr["BlogContent"] + "");
                    //将内容中的html代码去掉
                    blogcontent = Tools.NoHTML(blogcontent);
                    string strblogcontent = Tools.GetFirstString(blogcontent, 160);
                    if (strblogcontent != "")
                    {
                        string bloguserid = dr["UsersID"] + "";
                        //加载头像
                        UsersHelper uhelper = new UsersHelper();
                        Users ubloginfo = uhelper.GetUsers(bloguserid);
                        string headphoto = ubloginfo.SmallHeadImage;
                        if (!string.IsNullOrEmpty(headphoto))
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"../u/" + bloguserid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"" + headphoto + "\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                        else
                        {
                            dr["strBlogContent"] = "<p class=\"post_item_summary\" ><a href=\"../u/" + bloguserid + ".aspx\" target=\"_blank\"><img align=\"left\" width=\"48\" height=\"48\"  class=\"pfs\" src=\"../images/morentouxiang.jpg\" alt=\"\"/></a>" + strblogcontent + "</p>";
                        }
                    }

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }

                rptBlog.DataSource = dt;
                rptBlog.DataBind();
                lblMessage.Visible = false;

                //判断当前用户是否为组长
                Users currentuinfo = UserBase.GetCurrentUser();
                if (currentuinfo != null)
                {
                    if (currentuinfo.ID == companyowneruserid)
                    {
                        for (int i = 0; i < rptBlog.Items.Count; i++)
                        {
                            LinkButton lbDeleteCompany = (LinkButton)rptBlog.Items[i].FindControl("lbDeleteCompany");
                            lbDeleteCompany.Visible = true;
                        }
                    }
                }
            }
            else
            {
                lblMessage.Visible = true;
                lblMessage.Text = "用户在该小组暂时没有创作，<a href=\"../Index.aspx\"><font color=\"red\">返回首页</font></a>";
            }


        }
        #endregion

        #region 加载SEO信息
        private void BindSeo()
        {

            string companyname = litCompanyName.Text;
            string systemkeyword = System.Configuration.ConfigurationManager.AppSettings["keyword"] + "";

            string author = System.Configuration.ConfigurationManager.AppSettings["author"] + "";
            string copyright = System.Configuration.ConfigurationManager.AppSettings["copyright"] + "";

            string keyword = companyname + " " + systemkeyword + " " + author;
            string description = litBrief.Text;
            string title = author + "-" + companyname;

            strseo += "<title>" + title + "</title>\r\n";
            strseo += "<meta name=\"keywords\" content=\"" + keyword + "\" />\r\n";
            strseo += "<meta name=\"description\" content=\"" + description + "\" />\r\n";
            strseo += "<meta name=\"author\" content=\"" + author + "\" />\r\n";
            strseo += "<meta name=\"Copyright\" content=\"" + copyright + "\" />\r\n";
        }
        #endregion

        #region 加载小组用户
        private void BindCompanyUser()
        {
            UserCompanyHelper uchelper = new UserCompanyHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "CompanyID", companyid);
            c.Add(CriteriaType.Equals, "Status", 1);

            DataTable dt = uchelper.QueryUserCompanyBySearchToTable(c, new Order[] { new Order("CreateDate", OrderMode.Desc) }, 0, 20, null);

            rptCompanyUser.DataSource = dt;
            rptCompanyUser.DataBind();
        }
        #endregion

        #region 加载该小组博客浏览榜
        private void BindPVBlogOrder()
        {
            BlogHelper bhelper = new BlogHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "Flag", 0);
            c.Add(CriteriaType.Equals, "CompanyID", companyid);

            string[] fields = new string[] { "ID", "BlogBigType", "HasPic", "Title", "UsersID", "PVCount", "Flag", "CreateDate" };
            DataTable dtpvBlog = bhelper.QueryBlogBySearchToTable(c, new Order[] { new Order("PVCount", OrderMode.Desc) }, 0, 15, fields);

            DataColumn dcTitle = new DataColumn("strTitle");
            dtpvBlog.Columns.Add(dcTitle);
            dtpvBlog.Columns.Add(new DataColumn("strHasPic"));
            dtpvBlog.Columns.Add(new DataColumn("strVideoPic"));

            if (dtpvBlog != null && dtpvBlog.Rows.Count > 0)
            {
                foreach (DataRow dr in dtpvBlog.Rows)
                {
                    string title = dr["title"] + "";
                    string strtitle = "";
                    strtitle = Tools.GetFirstString(title, 26);
                    dr["strTitle"] = strtitle;

                    //判断是否存在图片
                    string haspic = dr["haspic"] + "";
                    if (haspic == "1")
                    {
                        dr["strHasPic"] = "<img src=\"../images/smallpic.jpg\" alt=\"此创作包含图片\"/>";
                    }
                    else
                    {
                        dr["strHasPic"] = "";
                    }

                    string blogbigtype = dr["BlogBigType"] + "";
                    if (blogbigtype == "视频")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/smallvideo.jpg\" alt=\"视频\"/>";
                    }
                    else if (blogbigtype.ToLower() == "mp3")
                    {
                        dr["strVideoPic"] = "<img src=\"../images/mp3.png\" alt=\"mp3\"/>";
                    }
                    else
                    {
                        dr["strVideoPic"] = "";
                    }
                }
            }
            rptPVBlogOrder.DataSource = dtpvBlog;
            rptPVBlogOrder.DataBind();
        }
        #endregion

        #region 加载组长关注的小组
        private void BindCareCompany()
        {
            string owneruserid = hidOwnerUserID.Value;
            UserCareHelper uchelper = new UserCareHelper();
            Criteria c = new Criteria();
            c.Add(CriteriaType.Equals, "UsersID", owneruserid);
            //c.Add(CriteriaType.NotEquals, "ObjectID", companyid);
            c.Add(CriteriaType.Equals, "ObjectType", 1);

            DataTable dt = uchelper.QueryUserCareBySearchToTable(c, null, 0, 15, null);
            dt.Columns.Add(new DataColumn("CompanyName"));
            dt.Columns.Add(new DataColumn("strCompanyName"));
            dt.Columns.Add(new DataColumn("BlogPVNum"));
            if (dt != null && dt.Rows.Count > 0)
            {
                foreach (DataRow dr in dt.Rows)
                {
                    string objectid = dr["objectid"] + "";
                    Companys cinfo = new Companys();
                    CompanysHelper chelper = new CompanysHelper();
                    cinfo = chelper.GetCompanys(objectid);
                    string companyname = cinfo.CompanyName;
                    dr["CompanyName"] = companyname;
                    dr["strCompanyName"] = Tools.GetFirstString(companyname, 20);
                    dr["BlogPVNum"] = cinfo.BlogPVNum.ToString();
                }
            }

            rptCareCompany.DataSource = dt;
            rptCareCompany.DataBind();
        }
        #endregion

        #region 列表事件
        protected void rptBlog_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            HiddenField hidID = (HiddenField)e.Item.FindControl("hidID");
            string id=hidID.Value;
            if (e.CommandName == "deletecompany")//清空该博客的所有小组
            {
                Blog binfo = new Blog();
                BlogHelper bhelper = new BlogHelper();
                binfo = bhelper.GetBlog(id);
                binfo.CompanyID = "0";
                bhelper.EditBlog(binfo);

                Tools.Alert("小组中删除成功");
                BindBlog();
            }
        }
        #endregion
    }
}